package com.example.spring_data_jpa2.dao;

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import com.example.spring_data_jpa2.domain.Customer;
/*
 * JpaRepository<对应的实体类，实体类中主键属性的类型>
 * JpaSpecificationExecutor<对应的实体类>
 */
public interface CustomerDao extends JpaRepository<Customer, Long>,JpaSpecificationExecutor<Customer>{

	//==================jpql方式=========================//
	/*
	 * 根据客户名称查询
	 * jpql:from Customer where custName=?
	 */ 
	@Query(value = "from Customer where custName=?")
	Customer findJpql(String custName);

	/*
	 * 根据客户名称和客户id查询
	 * jpql:from Customer where custName=? and custId=?
	 * 默认:占位符位置和方法中参数的位置要一致
	 */
	@Query(value="from Customer where custName=? and custId=?")
	Customer findCustNameAndId(String name,Long id);
	
	/*
	 * 指定占位符参数的位置
	 * ？参数索引
	 */
	@Query(value="from Customer where custName=?2 and custId=?1")
	Customer findCustNameAndId2(Long id,String name);
	
	/*
	 * 根据id更新客户的名称
	 *  sql: update cst_customer set cust_name= ? where cust_id=?
	 * jpql: update Customer set custName=? where custId=?
	 */
	@Query(value = "update Customer set custName=?2 where custId=?1")
	@Modifying //指定方法为更新操作
	void updateCustomer(Long custid,String custName);
	
	
	
	
	//==================本地sql方式=========================//
	
	/*
	 * 使用sql的形式查询
	 * nativeQuery = true使用本地的sql方式查询
	 */
	//@Query(value = "select * from cst_customer",nativeQuery = true)
	@Query(value = "select * from cst_customer where cust_name like?1",nativeQuery = true)
	List<Customer> findSql(String name);
	
	
	
	
	
	//==================使用特定方法命名方式=========================//
	/*
	 * 限定方法名的查询，不需要写jpql
	 * findBy+对象的属性名称(首字母大写)
	 */
	Customer findByCustName(String name);
	
	/*
	 * 模糊查询:在上面的基础上加Like
	 * findBy+对象的属性名称(首字母大写)+Like
	 */
	List<Customer> findByCustNameLike(String name);
	
	/*
	 * 客户名模糊匹配和客户行业精准匹配
	 * findBy+对象的属性名称(首字母大写)+Like+条件连接符(and|or)+属性名+"查询方式"
	 */
	Customer findByCustNameLikeAndCustIndustry(String custName,String custIndustry);
	
}
